package com.hakim.wfb.web.controller;

import com.alibaba.fastjson.JSON;
import com.hkfs.fundamental.common.utils.StrUtils;
import com.hkfs.fundamental.servlet.utils.ActionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * Created by zhoubing on 2016/5/19.
 */
public class BaseController implements Serializable {
    protected Logger logger = LoggerFactory.getLogger(BaseController.class);

    protected <T> T getRequestObject(HttpServletRequest request, Class<T> cls) {

        String data = ActionUtils.getContentFromRequest(request, StrUtils.UTF_8);

        if (StrUtils.isEmpty(data)) {
            logger.error("请求数据为空");
            return null;
        }
        if (!data.startsWith("{") || !data.endsWith("}")) {
            logger.error("请求数据格式错误");
            return null;
        }


        return JSON.parseObject(data, cls);
    }
}
